package com.yingxue.lesson.controller;

import com.yingxue.lesson.entity.SysUser;
import com.yingxue.lesson.entity.User;
import com.yingxue.lesson.service.UserService;
import com.yingxue.lesson.vo.req.RegisterReqVO;
import com.yingxue.lesson.vo.req.UpdateUserReqVO;
import com.yingxue.lesson.vo.req.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

/**
 * @Author gusheng
 * @Date 2021/1/5 14:53
 * @ClassName 类名称
 * @Description 类描述
 * @Version 3.4
 */
@Controller
@RequestMapping("/sys")
@Api(tags = "用户模块")
public class UserController {
    @Autowired
    private UserService userService;


    @ApiOperation("数据库初始化100条数据")
    @RequestMapping(value = "/init", method = RequestMethod.GET)
    @ResponseBody
    public void init() {
        for (int i = 0; i < 100; i++) {
            Random rand = new Random();
            User user = new User();
            String temp = "un" + i;
            user.setUsername(temp);
            user.setPassword(temp);
            int n = rand.nextInt(2);
            user.setSex((byte) n);
            userService.createUser(user);
        }
    }

    @ApiOperation("单个用户查询，按userid查用户信息")
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    @ResponseBody
    public UserVO findById(@PathVariable int id) {
        User user = this.userService.findUserById(id);
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        return userVO;
    }

    @ApiOperation("修改某条数据")
    @PostMapping(value = "/updateUser")
    @ResponseBody
    public void updateUser(@RequestBody UserVO obj) {
        User user = new User();
        BeanUtils.copyProperties(obj, user);
        userService.updateUser(user);
    }


    @GetMapping("/getUser")
    @ResponseBody
    public SysUser getUser(@RequestParam(value = "id",required = false) String id){
        return userService.getUserInfo(id);
    }

    /**
     * restful风格查一个用户
     * @param id
     * @return
     */
    @GetMapping("/user/{id}")
    @ResponseBody
    @ApiOperation("restful风格查一个用户")
    public SysUser getUserDetail(@PathVariable("id") String id){
        return userService.getUserInfo(id);
    }

    /**
     * restful风格增加一个用户
     * @param vo
     * @return
     */
    @PostMapping("/user")
    @ResponseBody
    @ApiOperation("restful风格增加一个用户")
    public String addUser(@RequestBody RegisterReqVO vo){
        return userService.register(vo);
    }

    /**
     * restful风格更新一个用户
     * @param vo
     * @return
     */
    @PutMapping("/user")
    @ResponseBody
    @ApiOperation("restful风格更新一个用户")
    public String updateUser(@RequestBody UpdateUserReqVO vo){
        return userService.updateUserInfo(vo);
    }

    /**
     * restful风格删除一个用户
     * @param id
     * @return
     */
    @DeleteMapping("/user/{id}")
    @ResponseBody
    @ApiOperation("restful风格删除一个用户")
    public String deleteUser(@PathVariable("id") String id){
        return userService.deleted(id);
    }
}
